import unittest
import pandas as pd
from analysis.analysis_preprocessing import preprocessing_data


class MyTestCase(unittest.TestCase):
    def test_something(self):
        df = self._prepare_data()
        ndf = preprocessing_data(df)
        ndf.to_csv('test-2.csv', index=False, encoding='utf-8-sig')

    def _prepare_data(self):
        data = """2023-11-22,sh.600178,8.0900,8.0900,7.8000,7.8000,8.1400,24617202,194108345.7100,3,5.327500,1,-4.176900,0
        2023-11-23,sh.600178,7.8600,8.5800,7.8200,8.5800,7.8000,30271620,257072182.6000,3,6.551200,1,10.000000,0
        2023-11-24,sh.600178,9.4400,9.4400,9.2300,9.4400,8.5800,24927214,235140016.1600,3,5.394600,1,10.023300,0
        2023-11-27,sh.600178,10.3800,10.3800,10.3800,10.3800,9.4400,4127573,42844207.7400,3,0.893300,1,9.957600,0
        2023-11-28,sh.600178,11.4200,11.4200,11.4200,11.4200,10.3800,4205866,48030989.7200,3,0.910200,1,10.019300,0
        2023-11-29,sh.600178,12.5600,12.5600,12.5600,12.5600,11.4200,6718994,84390564.6400,3,1.454100,1,9.982500,0
        2023-11-30,sh.600178,12.5500,13.8200,12.1000,13.8200,12.5600,133751911,1792865181.9700,3,28.945600,1,10.031800,0
        2023-12-01,sh.600178,13.9900,15.2000,12.4400,15.2000,13.8200,132634284,1800016854.7100,3,28.703700,1,9.985500,0
        2023-12-04,sh.600178,15.5000,16.7200,15.4800,16.7200,15.2000,132314455,2166820954.0300,3,28.634500,1,10.000000,0
        2023-12-05,sh.600178,17.8900,18.3900,15.8500,18.3900,16.7200,104193041,1820153385.2300,3,22.548700,1,9.988000,0
        2023-12-06,sh.600178,19.3000,20.2300,16.5500,16.5500,18.3900,152966461,2860308323.2300,3,33.103900,1,-10.005400,0"""
        # 将数据字符串分割成行
        data_lines = data.strip().split("\n")

        # 创建一个空的DataFrame
        df = pd.DataFrame()

        # 遍历每一行，分割数据，并添加到DataFrame中
        for line in data_lines:
            # 使用逗号分割每一行的数据
            values = line.split(",")
            # 确保数据分割正确
            if len(values) == 14:
                # 创建一个临时字典来存储这一行的数据
                row = {
                    'date': pd.to_datetime(values[0]),
                    'code': values[1],
                    'open': float(values[2]),
                    'high': float(values[3]),
                    'low': float(values[4]),
                    'close': float(values[5]),
                    'preclose': float(values[6]),
                    'volume': int(values[7]),
                    'amount': float(values[8]),
                    'adjustflag': int(values[9]),
                    'turn': float(values[10]),
                    'tradestatus': int(values[11]),
                    'pctChg': float(values[12]),
                    'isST': int(values[13])
                }
                # 使用concat方法将这一行的数据添加到DataFrame中
                df = pd.concat([df, pd.DataFrame([row])], ignore_index=True)
        return df



if __name__ == '__main__':
    unittest.main()
